import axios from 'axios'
axios.defaults.withCredentials=true
export default function ajax(url,data={},type="GET") {
  let promise;
  if (type === 'GET') {
    let dataStr = '';
    Object.keys(data).forEach(key => {
      dataStr += key + '=' + data[key] + '&'
    });
    if (dataStr !== '') {
      dataStr = dataStr.substring(0, dataStr.lastIndexOf('&'))
      url = url + '?' + dataStr
    }
    promise = axios.get(url)
  } else {
    promise = axios.post(url, data)
  }
  let dataPro = new Promise((resolve, reject) => {
    promise.then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err)
    })
  })

  return dataPro
}
